package com.fiberhome.ztelauncher.core.startpost;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.fiberhome.ztelauncher.core.MainActivity;
import com.fiberhome.zte_launcher.R;
import com.fiberhome.ztelauncher.util.BitmapUitl;

import java.util.ArrayList;
import java.util.List;

/**
 * 启动页
 */
public class SplashActivity extends Activity {

    private ViewPager viewpager;
    private RadioGroup radiogroup;
    private List<ImageView> imageViewList = new ArrayList<ImageView>();
    private List<String> listUrl = new ArrayList<String>();
    private int index;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        viewpager = (ViewPager) findViewById(R.id.viewpager);
        radiogroup = (RadioGroup) findViewById(R.id.radiogroup);

//        listUrl = getIntent().getStringArrayListExtra("imgUrl");
        listUrl.add("720p_en/assets/images/thematic/background/552a0958-f944-428e-a4c1-3255df55ee0b.jpg");
        listUrl.add("720p_en/assets/images/Ads_Banner/8aa1804a-d77a-4d28-9d31-da7a1945b3e0.jpg");
        listUrl.add("720p_en/assets/images/Ads_Banner/194813cb-f64f-4181-b326-a513db415256.jpg");
        listUrl.add("720p_en/assets/images/Ads_Banner/8a3a89fa-812a-4b14-b8a5-0e0dc909f60c.jpg");
        listUrl.add("720p_en/assets/images/Ads_Banner/d590bf3c-c4a6-419e-8677-093095e51b48.jpg");

        for (int i = 0; i < listUrl.size(); i++) {
            ImageView imageView = new ImageView(this);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageViewList.add(imageView);
            addNavigation(i);
        }
        ImagePagerAdapter adapter = new ImagePagerAdapter();
        viewpager.setAdapter(adapter);

        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                index = position;
                RadioButton rb = (RadioButton) radiogroup.getChildAt(position);
                rb.setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    /**
     * 添加指示器
     * 每一页添加一个RadioButton，默认选中第一个
     *
     * @param index
     */
    private void addNavigation(final int index) {
        RadioButton rb = new RadioButton(this);
        rb.setButtonDrawable(new BitmapDrawable(getResources(), (Bitmap) null));
        rb.setBackgroundResource(R.drawable.selector_indicator);
        RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(8, 8);
        params.setMargins(6, 6, 6, 6);
        params.gravity = Gravity.CENTER;
        radiogroup.addView(rb, params);
        if (index == 0) {
            rb.setChecked(true);
        }
    }


    class ImagePagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return listUrl.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return object == view;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(imageViewList.get(position));
            BitmapUitl.loadBitmap(SplashActivity.this, imageViewList.get(position), listUrl.get(position));
            return imageViewList.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(imageViewList.get(position));
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
            startActivity(new Intent(this, MainActivity.class));
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * @param context
     * @param listUrl
     */
    public static void goInto(Context context, ArrayList<String> listUrl) {
        Intent intent = new Intent(context, SplashActivity.class);
        intent.putStringArrayListExtra("imgUrl", listUrl);
        context.startActivity(intent);
    }
}
